﻿using System;
using System.IO;
using System.Collections.Generic;

namespace PBO.Data
{
    /// <summary>
    /// Stream Item
    /// </summary>
    public abstract class DataStream
    {
        public DataStream() { }

        public abstract void BinaryLoad(BinaryReader reader);
        public abstract void BinarySave(BinaryWriter writer);

        public void Load(Stream input)
        {
            var reader = new BinaryReader(input);
            this.BinaryLoad(reader);
        }

        public void Save(Stream output)
        {
            var writer = new BinaryWriter(output);
            this.BinarySave(writer);
        }

        internal static T FromBinary<T>(BinaryReader reader) where T : DataStream
        {
            var obj = Activator.CreateInstance<T>();
            obj.BinaryLoad(reader);
            return obj;
        }

        internal static T FromStream<T>(Stream stream) where T : DataStream
        {
            var obj = Activator.CreateInstance<T>();
            obj.Load(stream);
            return obj;
        }
    }
}
